package uranium.gui.add;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JDialog;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JOptionPane;

import uranium.db.DBManager;
import java.awt.Rectangle;

public class AddNewFornGUI extends JDialog{

	private JFrame jFrame = null;  //  @jve:decl-index=0:visual-constraint="251,30"
	private JPanel jContentPane = null;
	private JPanel jPanel = null;
	private JTextField jTextField = null;
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;
	private JTextField jTextField1 = null;
	private JLabel jLabel2 = null;
	private JTextField jTextField2 = null;
	private JLabel jLabel3 = null;
	private JTextField jTextField3 = null;
	private JLabel jLabel4 = null;
	private JTextField jTextField4 = null;
	private JLabel jLabel5 = null;
	private JLabel jLabel6 = null;
	private JButton jButton1 = null;
	private JTextField jTextField5 = null;
	private JTextField jTextField6 = null;
	private JLabel jLabel7 = null;
	private JLabel jLabel8 = null;
	private JLabel jLabel9 = null;
	private JTextField jTextField7 = null;
	private JTextField jTextField8 = null;
	private JTextField jTextField9 = null;
	private JButton jButton = null;
	private DBManager dbm = null;
	private JLabel jLabel10 = null;
	
	public AddNewFornGUI(DBManager dbm, JFrame jFrame){
		super(jFrame, true);
		this.jFrame = jFrame;
		this.dbm = dbm;
		initialize();
	}
	
	private void initialize(){
		/*jFrame = new JFrame();
		jFrame.setSize(new Dimension(348, 353));
		jFrame.setTitle("Inserisci Fornitore");
		jFrame.setPreferredSize(new Dimension(250, 188));
		jFrame.setContentPane(getJContentPane());
		centro();
		jFrame.setVisible(true);*/
		
		/*jFrame = new JFrame();*/
		this.setSize(new Dimension(348, 353));
		this.setTitle("Inserisci Fornitore");
		this.setPreferredSize(new Dimension(250, 188));
		this.setContentPane(getJContentPane());
		centro();
		this.setVisible(true);
	}
	
	private void centro()
	{
		int x = (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() - this.getWidth()) / 2;
		int y = (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight() - this.getHeight()) / 2;
		this.setLocation(x, y);
	}

	/**
	 * This method initializes jContentPane	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJPanel(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jLabel10 = new JLabel();
			jLabel10.setBounds(new Rectangle(15, 246, 120, 16));
			jLabel10.setText("* Campi Obbligatori");
			jLabel9 = new JLabel();
			jLabel9.setText("Note");
			jLabel9.setBounds(new Rectangle(15, 224, 36, 16));
			jLabel8 = new JLabel();
			jLabel8.setText("e-mail");
			jLabel8.setBounds(new Rectangle(15, 180, 45, 16));
			jLabel7 = new JLabel();
			jLabel7.setText("Fax");
			jLabel7.setBounds(new Rectangle(15, 158, 30, 16));
			jLabel6 = new JLabel();
			jLabel6.setText("Telefono");
			jLabel6.setBounds(new Rectangle(15, 136, 59, 16));
			jLabel5 = new JLabel();
			jLabel5.setText("Citta'");
			jLabel5.setBounds(new Rectangle(15, 114, 40, 16));
			jLabel4 = new JLabel();
			jLabel4.setText("CAP");
			jLabel4.setBounds(new Rectangle(15, 92, 34, 16));
			jLabel3 = new JLabel();
			jLabel3.setText("Indirizzo");
			jLabel3.setBounds(new Rectangle(15, 70, 57, 16));
			jLabel2 = new JLabel();
			jLabel2.setText(" Rapp. Aziendale *");
			jLabel2.setBounds(new Rectangle(15, 202, 103, 16));
			jLabel1 = new JLabel();
			jLabel1.setText(" Ragione Sociale *");
			jLabel1.setBounds(new Rectangle(15, 48, 104, 16));
			jLabel = new JLabel();
			jLabel.setText(" Partita IVA *");
			jLabel.setBounds(new Rectangle(15, 26, 73, 16));
			jPanel = new JPanel();
			jPanel.setLayout(null);
			jPanel.setPreferredSize(new Dimension(250, 166));
			jPanel.add(getJTextField(), null);
			jPanel.add(jLabel, null);
			jPanel.add(jLabel1, null);
			jPanel.add(getJTextField1(), null);
			jPanel.add(jLabel2, null);
			jPanel.add(getJTextField2(), null);
			jPanel.add(jLabel3, null);
			jPanel.add(getJTextField3(), null);
			jPanel.add(jLabel4, null);
			jPanel.add(getJTextField4(), null);
			jPanel.add(jLabel5, null);
			jPanel.add(jLabel6, null);
			jPanel.add(getJButton1(), null);
			jPanel.add(getJTextField5(), null);
			jPanel.add(getJTextField6(), null);
			jPanel.add(jLabel7, null);
			jPanel.add(jLabel8, null);
			jPanel.add(jLabel9, null);
			jPanel.add(getJTextField7(), null);
			jPanel.add(getJTextField8(), null);
			jPanel.add(getJTextField9(), null);
			jPanel.add(getJButton(), null);
			jPanel.add(jLabel10, null);
		}
		return jPanel;
	}

	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
			jTextField.setPreferredSize(new Dimension(200, 22));
			jTextField.setBounds(new Rectangle(130, 23, 200, 22));
		}
		return jTextField;
	}

	/**
	 * This method initializes jTextField1	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField1() {
		if (jTextField1 == null) {
			jTextField1 = new JTextField();
			jTextField1.setPreferredSize(new Dimension(200, 22));
			jTextField1.setBounds(new Rectangle(130, 45, 200, 22));
		}
		return jTextField1;
	}

	/**
	 * This method initializes jTextField2	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField2() {
		if (jTextField2 == null) {
			jTextField2 = new JTextField();
			jTextField2.setPreferredSize(new Dimension(200, 22));
			jTextField2.setBounds(new Rectangle(130, 199, 200, 22));
		}
		return jTextField2;
	}

	/**
	 * This method initializes jTextField3	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField3() {
		if (jTextField3 == null) {
			jTextField3 = new JTextField();
			jTextField3.setPreferredSize(new Dimension(200, 22));
			jTextField3.setBounds(new Rectangle(130, 67, 200, 22));
		}
		return jTextField3;
	}

	/**
	 * This method initializes jTextField4	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField4() {
		if (jTextField4 == null) {
			jTextField4 = new JTextField();
			jTextField4.setPreferredSize(new Dimension(200, 22));
			jTextField4.setBounds(new Rectangle(130, 89, 200, 22));
		}
		return jTextField4;
	}

	/**
	 * This method initializes jButton1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setText("Inserisci");
			jButton1.setBounds(new Rectangle(200, 270, 83, 26));
			jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					try{
						inserisci();
					}
					catch(Exception e23){
						System.err.println(e23);
					}
				}
			});
		}
		return jButton1;
	}

	/**
	 * This method initializes jTextField5	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField5() {
		if (jTextField5 == null) {
			jTextField5 = new JTextField();
			jTextField5.setPreferredSize(new Dimension(200, 22));
			jTextField5.setBounds(new Rectangle(130, 111, 200, 22));
		}
		return jTextField5;
	}

	/**
	 * This method initializes jTextField6	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField6() {
		if (jTextField6 == null) {
			jTextField6 = new JTextField();
			jTextField6.setPreferredSize(new Dimension(200, 22));
			jTextField6.setBounds(new Rectangle(130, 133, 200, 22));
		}
		return jTextField6;
	}

	/**
	 * This method initializes jTextField7	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField7() {
		if (jTextField7 == null) {
			jTextField7 = new JTextField();
			jTextField7.setPreferredSize(new Dimension(200, 22));
			jTextField7.setBounds(new Rectangle(130, 155, 200, 22));
		}
		return jTextField7;
	}

	/**
	 * This method initializes jTextField8	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField8() {
		if (jTextField8 == null) {
			jTextField8 = new JTextField();
			jTextField8.setPreferredSize(new Dimension(200, 22));
			jTextField8.setBounds(new Rectangle(130, 177, 200, 22));
		}
		return jTextField8;
	}

	/**
	 * This method initializes jTextField9	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField9() {
		if (jTextField9 == null) {
			jTextField9 = new JTextField();
			jTextField9.setPreferredSize(new Dimension(200, 22));
			jTextField9.setBounds(new Rectangle(130, 221, 200, 22));
		}
		return jTextField9;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setText("Annulla");
			jButton.setBounds(new Rectangle(70, 270, 83, 26));
			jButton.setPreferredSize(new Dimension(83, 26));
			jButton.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					try{
						close();
					}
					catch(Exception e24){
						System.err.println(e24);
					}
				}
			});
		}
		return jButton;
	}
	private void close(){
		this.dispose();
	}
	
	private synchronized void inserisci(){
		boolean error = false;
		if(jTextField.getText().trim().equals("")){
			JOptionPane.showMessageDialog(jFrame, "Campo Partita IVA non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}
		if(jTextField1.getText().trim().equals("")){
			JOptionPane.showMessageDialog(jFrame, "Campo Ragione Sociale non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}
		if(jTextField2.getText().trim().equals("")){
			JOptionPane.showMessageDialog(jFrame, "Campo Rappresentanza Aziendale non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}
		
		if(error == false){
			String piva = jTextField.getText();
			String ragsoc = jTextField1.getText();
			String indirizzo = jTextField3.getText();
			String cap = jTextField4.getText();
			String citta = jTextField5.getText();
			String tel = jTextField6.getText();
			String fax = jTextField7.getText();
			String email = jTextField8.getText();
			String rappraz = jTextField2.getText();
			String note = jTextField9.getText();
			String insert="INSERT INTO fornitori (piva,ragsociale,indirizzo,cap,citta,tel,fax,email,rappaz,note) VALUES('"+piva+"','"+ragsoc+"','"+indirizzo+"','"+cap+"','"+citta+"','"+tel+"','"+fax+"','"+email+"','"+rappraz+"','"+note+"')";
		
			dbm.executeQuery(insert);
			this.dispose();
		}
	}	

}
